package com.linx.message.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.linx.common.core.domain.R;
import com.linx.common.core.validate.AddGroup;
import com.linx.common.core.validate.EditGroup;
import com.linx.common.excel.utils.ExcelUtil;
import com.linx.common.idempotent.annotation.RepeatSubmit;
import com.linx.common.log.annotation.Log;
import com.linx.common.log.enums.BusinessType;
import com.linx.common.mybatis.core.page.PageQuery;
import com.linx.common.mybatis.core.page.TableDataInfo;
import com.linx.common.web.core.BaseController;
import com.linx.message.domain.bo.MessageInfoBo;
import com.linx.message.domain.vo.MessageInfoVo;
import com.linx.message.service.IMessageInfoService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息
 *
 * @author linx
 * @date 2024-11-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/message/message-info")
public class MessageInfoController extends BaseController {

    private final IMessageInfoService messageInfoService;

    /**
     * 查询消息列表
     */
    @SaCheckPermission("message:message-info:list")
    @GetMapping("/list")
    public TableDataInfo<MessageInfoVo> list(MessageInfoBo bo, PageQuery pageQuery) {
        return messageInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出消息列表
     */
    @SaCheckPermission("message:message-info:export")
    @Log(title = "消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MessageInfoBo bo, HttpServletResponse response) {
        List<MessageInfoVo> list = messageInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "消息", MessageInfoVo.class, response);
    }

    /**
     * 获取消息详细信息
     *
     * @param messageInfoId 主键
     */
    @SaCheckPermission("message:message-info:query")
    @GetMapping("/{messageInfoId}")
    public R<MessageInfoVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long messageInfoId) {
        return R.ok(messageInfoService.queryById(messageInfoId));
    }

    /**
     * 新增消息
     */
    @SaCheckPermission("message:message-info:add")
    @Log(title = "消息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MessageInfoBo bo) {
        return toAjax(messageInfoService.insertByBo(bo));
    }

    /**
     * 修改消息
     */
    @SaCheckPermission("message:message-info:edit")
    @Log(title = "消息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MessageInfoBo bo) {
        return toAjax(messageInfoService.updateByBo(bo));
    }

    /**
     * 删除消息
     *
     * @param messageInfoIds 主键串
     */
    @SaCheckPermission("message:message-info:remove")
    @Log(title = "消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{messageInfoIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] messageInfoIds) {
        return toAjax(messageInfoService.deleteWithValidByIds(List.of(messageInfoIds), true));
    }
}
